package mya.mes.mdc.model.tag.convert;


import mya.mes.mdc.model.excel.ExcelTagAlarmDef;
import mya.mes.mdc.model.tag.AlarmTag;
import mya.mes.mdc.model.tag.Shop;
import mya.mes.mdc.model.tag.ViewTag;

import java.util.List;

class AlarmTagParse {

  private final AlarmTag tagInstance;

  public AlarmTagParse() {
    this.tagInstance = new AlarmTag();
  }


  public AlarmTag convert(String shop, String area, AlarmTagTmp tmp) {
    if (tmp.list == null || tmp.list.isEmpty()) {
      throw new RuntimeException(tmp.tagName + "没有异常定义");
    }
    setMdcTag(tmp.tagName);
    setAlarmDefRows(shop, area, tmp.list);
    return this.tagInstance;
  }


  private void setMdcTag(String rdbTag) {
    if (rdbTag == null) {
      throw new IllegalArgumentException("rdbTag不能为null");
    }
    rdbTag = rdbTag.trim();
    if (rdbTag.isEmpty()) {
      throw new IllegalArgumentException("rdbTag不能为空");
    }
    tagInstance.mdcTag = rdbTag;
    if (tagInstance.mdcTag.length() < 2) {
      throw new IllegalArgumentException("rdbTag长度小于2");
    }
    tagInstance.tagPrefix = tagInstance.mdcTag.substring(0, 2);
    try {
      tagInstance.tagIndex = Integer.parseInt(rdbTag.substring(2));
    } catch (Exception e) {
      throw new IllegalArgumentException("rdbTag不能转换为数字: " + rdbTag);
    }
  }

  public void setAlarmDefRows(String shop, String area, List<ExcelTagAlarmDef> tagAlertDefList) {
    tagInstance.setExcelTagAlarmRowList(tagAlertDefList);
    tagInstance.viewTag = getViewTag(shop, area, tagAlertDefList.get(0));
  }

  private ViewTag getViewTag(String shop, String area, ExcelTagAlarmDef row) {
    return getViewTagZh(shop, area, row);
//    if (row == null) {
//      return null;
//    }
//    String en = row.enUs;
//
//    if (en == null) {
//      return getViewTagZh(row);
//    }
//
//    en = en.replace("：", ":");
//    ViewTag viewTag = new ViewTag();
//
//    int firstIndex = en.indexOf(':');
//    if (firstIndex < 0) {
//      throw new RuntimeException("startIndex < 0");
//    }
//    int secondIndex = en.indexOf(':', firstIndex + 1);
//
//    if (secondIndex < 0) {
//      throw new RuntimeException("secondIndex < 0");
//    }
//    String str1 = en.substring(0, firstIndex).trim();
//    String str2 = en.substring(firstIndex + 1, secondIndex).trim();
//
//    if (en.startsWith("Section")) {
//
//      viewTag.section = str1;
//      viewTag.system = false;
//
//      if (str2.startsWith("Fault") || str2.startsWith("Alarm")) {
//        viewTag.tagName = str2;
//      } else {
//
//        int split = str2.indexOf('.');
//
//        viewTag.tagName = str2.substring(0, split);
//      }
//
//    } else if (en.startsWith("System")) {
//      viewTag.system = true;
//      viewTag.tagName = str2;
//
//    } else {
//      throw new RuntimeException("不能理解的TAG： + " + en);
//    }
//
//
//    return viewTag;
  }

  private ViewTag getViewTagZh(String shop, String area, ExcelTagAlarmDef row) {
    if (row == null) {
      return null;
    }
    if (Shop.CUR.equals(shop) && "BFG1".equals(area)) {
      ViewTag viewTag = new ViewTag();
      viewTag.section = "BFG";
      viewTag.system = false;
      viewTag.tagName = "BFG1";
      return viewTag;
    }else if (Shop.CUR.equals(shop) && "BFG2".equals(area)) {
      ViewTag viewTag = new ViewTag();
      viewTag.section = "BFG";
      viewTag.system = false;
      viewTag.tagName = "BFG2";
      return viewTag;
    }else if (Shop.CUR.equals(shop) && "BFG3".equals(area)) {
      ViewTag viewTag = new ViewTag();
      viewTag.section = "BFG";
      viewTag.system = false;
      viewTag.tagName = "BFG3";
      return viewTag;
    } else if (Shop.CUR.equals(shop) && "BFG4".equals(area)) {
      ViewTag viewTag = new ViewTag();
      viewTag.section = "BFG";
      viewTag.system = false;
      viewTag.tagName = "BFG4";
      return viewTag;
    }
    String en = row.zhCN;
    en = en.replace("：", ":");
    ViewTag viewTag = new ViewTag();
    int firstIndex = en.indexOf(':');
    if (firstIndex < 0) {
      throw new RuntimeException("startIndex < 0");
    }
    int secondIndex = en.indexOf(':', firstIndex + 1);
    if (secondIndex < 0) {
      throw new RuntimeException("secondIndex < 0");
    }
    String str1 = en.substring(0, firstIndex).trim();
    String str2 = en.substring(firstIndex + 1, secondIndex).trim();
    if (en.contains("区域") || en.contains("Section") || en.contains("OVEN")) {
      viewTag.section = str1;
      viewTag.system = false;
      if (str2.startsWith("故障") || str2.startsWith("Alarm")) {
        viewTag.tagName = str2;
      } else {
        int split = str2.indexOf('.');
        viewTag.tagName = str2.substring(0, split);
      }
    } else if (en.startsWith("系统")) {
      viewTag.system = true;
      viewTag.tagName = str2;
    } else {
      throw new RuntimeException("不能理解的TAG： + " + en);
    }
    return viewTag;
  }
}
